<?php
/**
 * Created by PhpStorm.
 * User: davin
 * Date: 14-10-19
 * Time: 下午9:25
 */


class BackCarouselService extends ServiceBase {


    public function getList($inputs = array(), $pageCount = null){
        //允许排序的字段名称数组
        $canOrderCols = array('status', 'type', 'updated_at', 'created_at');

        $order = $this->getSearchField($inputs, 'order');
        $reverse = $this->getSearchField($inputs, 'reverse', 0 );
        $page = $this->getSearchField($inputs, 'page', 1 );

        $description = str_replace(' ', '%', $this->getSearchField($inputs,  'description', '' ));
        $status = $this->getSearchField($inputs,  'status', null );
        $type = $this->getSearchField($inputs,  'type', null );
        $created_at_start = $this->getSearchField($inputs,  'created_at_end', null );
        $created_at_end = $this->getSearchField($inputs,  'created_at_end', null );
        $updated_at_start = $this->getSearchField($inputs,  'updated_at_start', null );
        $updated_at_end = $this->getSearchField($inputs,  'updated_at_end', null );


        $entries = Carousel::orderby(in_array($order, $canOrderCols)?$order:'updated_at', $reverse == 0?'desc':'asc');

        if(!empty($description)){
            $entries = $entries->where('description', 'like', '%'.$description.'%');
        }

        if(!empty($status)){
            $entries = $entries->whereIn('status', array_keys($status));
        }

        if(!empty($type)){
            $entries = $entries->whereIn('type', array_keys($type));
        }

        if(!empty($created_at_start)){
            $created_at_start = strtotime($created_at_start);
            $created_at_start = date('Y-m-d', $created_at_start).' 00:00:00';

            $entries = $entries->where('created_at', '>=', $created_at_start);
        }

        if(!empty($created_at_end)){
            $created_at_end = strtotime($created_at_end);
            $created_at_end = date('Y-m-d', $created_at_end).' 23:59:59';

            $entries = $entries->where('created_at', '<=', $created_at_end);
        }

        if(!empty($updated_at_start)){
            $updated_at_start = strtotime($updated_at_start);
            $updated_at_start = date('Y-m-d', $updated_at_start).' 00:00:00';

            $entries = $entries->where('updated_at', '>=', $updated_at_start);
        }

        if(!empty($updated_at_end)){
            $updated_at_end = strtotime($updated_at_end);
            $updated_at_end = date('Y-m-d', $updated_at_end).' 23:59:59';

            $entries = $entries->where('updated_at', '<=', $updated_at_end);
        }

        $pageCount = is_numeric($pageCount) ? $pageCount : Config::get('app.pagenate_num');
        $entries = $entries->paginate($pageCount)
            ->addQuery('order',$order)
            ->addQuery('reverse',$reverse)

            ->addQuery('description',$description)
            ->addQuery('status',$status)
            ->addQuery('type',$type);

        $links = array(
            "order"=>$order,
            "reverse"=>$reverse,
            "page"=>$page,
            'description' => $description,
            'status' => $status,
            'type' => $type,
        );

        $status_for_display = $this->prepareCheckboxDisplayForSearch(Carousel::$statuses,'display_name', $status);
        $type_for_display = $this->prepareCheckboxDisplayForSearch(Carousel::$types,'display_name', $type);

        return array(
            'entries'=>$entries,
            'links'=>$links,
            'status_for_display'=>$status_for_display,
            'type_for_display'=>$type_for_display,
        );

    }

    public function createCarousel(){
        $carousel = new Carousel();
        $this->parseInput($carousel,'status');
        $this->parseInput($carousel,'type');
        $this->parseInput($carousel,'description','');

        if(!$carousel->save()){
            $this->errors()->merge($carousel->errors());
            return false;
        }

        return true;
    }

    public function updateCarousel($carousel){

        $this->parseInput($carousel,'status');
        $this->parseInput($carousel,'type');
        $this->parseInput($carousel,'description');

        if(!$carousel->save()){
            $this->errors()->merge($carousel->errors());
            return false;
        }

        return true;
    }

    public function deleteCarousel($carousel){
        if(!$carousel->delete()){
            $this->errors()->merge($carousel->errors());
            return false;
        }
        return true;
    }

    private function getSearchField($inputs, $key, $default = null){
        return isset($inputs[$key]) ? $inputs[$key] : Input::get($key, $default);
    }


}

